/**
 * Created by YY on 16/9/26.
 */
angular.module('ProductDisplay')
    .controller('ProductDisplay.DepartmentCtrl', ['$scope', '$state', '$ionicScrollDelegate', 'Http', 'imanDialog', 'ProductDisplay.Service',
        function ($scope, $state, $ionicScrollDelegate, Http, imanDialog, ProductService) {
            $scope.index = 1;
            $scope.mm = {};
            $scope.nn = {};
            $scope.obj = {};
            var count = 1;
            $scope.$on('$ionicView.afterEnter', function () {
                angular.element(document).keydown(onkeydown);
                ProductService.queryDepartment().then(function (records) {
                    $scope.departs = records;
                    angular.forEach(records, function (key, value) {
                        angular.forEach(key, function (data, index) {
                            $scope.obj[count] = value + (index + 1);
                            $scope.mm[count] = data;
                            $scope.nn[count] = value;
                            count++;
                        });
                    });
                });
            });

            $scope.$on('$ionicView.beforeLeave', function () {
                angular.element(document).off('keydown', onkeydown);
            });

            $scope.selectDepart = function (value) {
                ProductService.setDepartment(value);
                $state.go('ProductDisplay');
            };

            function onkeydown(event) {
                var keyCode = event.keyCode;
                var height = 0;
                if (keyCode === 38) { //上
                    if ($scope.index > 1) {
                        height = angular.element('#' + $scope.obj[$scope.index])[0].offsetHeight;
                        if ($scope.index > 1 && $scope.nn[$scope.index] === $scope.nn[$scope.index - 1]) {
                            if ($ionicScrollDelegate.$getByHandle('departScroll').getScrollPosition().top > height) {
                                $ionicScrollDelegate.scrollBy(0, -height, true);
                            } else {
                                $ionicScrollDelegate.scrollTop(true);
                            }
                        } else if ($scope.index > 1 && $scope.nn[$scope.index] !== $scope.nn[$scope.index - 1]) {
                            if ($ionicScrollDelegate.$getByHandle('departScroll').getScrollPosition().top > (height + 38)) {
                                $ionicScrollDelegate.scrollBy(0, -(height + 38), true);
                            } else {
                                $ionicScrollDelegate.scrollTop(true);
                            }
                        }
                        angular.element('#' + $scope.obj[$scope.index]).removeClass("highlight-activity");
                        $scope.index -= 1;
                        angular.element('#' + $scope.obj[$scope.index]).addClass("highlight-activity");
                    }
                } else if (keyCode === 40) { //下
                    if ($scope.index + 1 < count) {
                        height = angular.element('#' + $scope.obj[$scope.index])[0].offsetHeight;
                        if ($ionicScrollDelegate.$getByHandle('departScroll').getScrollView().__contentHeight > ($ionicScrollDelegate.$getByHandle('departScroll').getScrollView().__clientHeight +
                            $ionicScrollDelegate.$getByHandle('departScroll').getScrollPosition().top)) {
                            if ($scope.nn[$scope.index + 1] && $scope.nn[$scope.index] === $scope.nn[$scope.index + 1]) {
                                $ionicScrollDelegate.scrollBy(0, height, true);
                            } else {
                                $ionicScrollDelegate.scrollBy(0, height + 38, true);
                            }
                        }
                        angular.element('#' + $scope.obj[$scope.index]).removeClass("highlight-activity");
                        $scope.index += 1;
                        angular.element('#' + $scope.obj[$scope.index]).addClass("highlight-activity");
                    }
                } else if (keyCode === 13) { // 确认
                    ProductService.setDepartment($scope.mm[$scope.index]);
                    $state.go('ProductDisplay');
                }
            }
        }
    ]);